#!/bin/bash
basepath=$(cd `dirname $0`; pwd)
. $basepath/common/common.sh

#0.检查执行用户是否是root
function checkRoot(){
	id | awk '{print $1}'|grep uid=0 >> /dev/null
	ASSERT $? "当前用户不是Root，无法运行这个脚本"
	echo "0. 确认当前用户是Root。"
}

#1.杀死java应用
function kill_java_apps(){
	echo "1. 开始杀死java应用(/usr/local/app)..."
	ps -ef|grep -v grep | grep /usr/local/app | awk '{print $2}' |xargs kill -9
	echo "#结束杀死java应用。"
}

#2.停止Mysql
function stopMySQL(){
	echo "2. 开始停止Mysql..."
	service mysqld stop;
	echo "#MySQL 已停止。"
}

#3.停止Oracle
function stopOracle(){
	echo "3. 开始停止Oracle..."
	ps -ef | grep ora_ | grep -v grep >> /dev/null
		if [[ ! 0 == $? ]]; then
			su - oracle <<EOF 

			lsnrctl start; 

			export ORACLE_SID=orcldb;
			sqlplus sys/ora as sysdba; 

			shutdown immediate;

			exit;
EOF

		fi

	echo "#Oracle 已停止。"	
}

#4.停止cassandra
function stopCassandra(){
	echo "4. 开始停止Cassandra..."
	service cassandra stop;
	echo "#Cassandra 已停止。"
}

#5.停止redis
function stopRedis(){
	echo "5. 开始停止Redis..."
	service redis1 stop;
	service redis2 stop;
	service redis3 stop;
	service redis4 stop;
	echo "#Redis 已停止。"
}

#6.停止Zookeeper
function stopZookeeper(){
	echo "6. 开始停止Zookeeper..."
	service zookeeper1 stop;

	service zookeeper2 stop;

	service zookeeper3 stop;

	service zookeeper4 stop;

	echo " Zookeeper 已停止。";	
}

#7.停止rabbitmq
function stopRabbitmq(){
	echo "7. 开始停止Rabbitmq..."
	service rabbitmq-server stop;
	echo "#Rabbitmq 已停止。"
}

#8.停止Mongodb
function stopMongodb(){
	echo "8. 开始停止Mongodb..."
	service mongodb stop;
	echo "#Mongodb 已停止。"	
}


#9.停止httpd
function stopHttpd(){
	echo "9. 开始停止Apache(httpd)..."
	service httpd stop;
	echo "#Apache(httpd) 已停止。"	
}

#10.停止vsftpd
function stopVsftpd(){
	echo "10. 开始停止FTP(vsftpd)..."
	service vsftpd stop;
	echo "#FTP(vsftpd) 已停止。"	
}


function main(){

	checkRoot;

	kill_java_apps;

	stopMySQL;

	stopOracle;

	stopCassandra;
	
	stopRedis;

	stopZookeeper;

	stopRabbitmq;

	stopMongodb;

	stopHttpd;

	stopVsftpd;

	SUCESS_EXIT "所有应用已经停止";

}

main;